package com.hghivln.controller.erp;


import com.hghivln.aspectj.annotation.Log;
import com.hghivln.aspectj.enums.BusinessType;
import com.hghivln.constants.Constants;
import com.hghivln.pojo.domain.SimpleUser;
import com.hghivln.pojo.dto.PurchaseDto;
import com.hghivln.pojo.dto.PurchaseFormDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.PurchaseService;
import com.hghivln.utils.IdGeneratorSnowflake;
import com.hghivln.utils.ShiroUtils;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("erp/purchase")
public class PurchaseController {

    @Reference
    private PurchaseService purchaseService;

    /**
     * 分页查询所有的采购单数据
     */
    @GetMapping("listPurchaseForPage")
    @HystrixCommand
    public AjaxResult listPurchaseForPage(PurchaseDto purchaseDto) {
        return purchaseService.listPurchaseForPage(purchaseDto);
    }

    /**
     * 分页查询所有的待审核采购单数据
     */
    @GetMapping("listPurchasePendingForPage")
    @HystrixCommand
    public AjaxResult listPurchasePendingForPage(PurchaseDto purchaseDto) {
        purchaseDto.setStatus(Constants.STOCK_PURCHASE_STATUS_2);
        return purchaseService.listPurchaseForPage(purchaseDto);
    }

    /**
     * 提交审核【根据采购单号】
     */
    @Log(title = "采购单管理--提交审核",businessType = BusinessType.UPDATE)
    @PostMapping("doAudit/{purchaseId}")
    @HystrixCommand
    public AjaxResult doAudit(@PathVariable @Validated @NotBlank(message = "采购单ID不能为空") String purchaseId) {
        SimpleUser simpleUser = ShiroUtils.getCurrentSimpleUser();
        return purchaseService.doAudit(purchaseId,simpleUser);
    }

    /**
     * 作废【根据采购单号】
     */
    @Log(title = "采购单管理--作废",businessType = BusinessType.DELETE)
    @PostMapping("doInvalid/{purchaseId}")
    @HystrixCommand
    public AjaxResult doInvalid(@PathVariable @Validated @NotBlank(message = "采购单ID不能为空") String purchaseId) {
        return purchaseService.doInvalid(purchaseId);
    }

    /**
     * 审核通过【根据采购单号】
     */
    @Log(title = "采购单管理--审核通过",businessType = BusinessType.UPDATE)
    @PostMapping("auditPass/{purchaseId}")
    @HystrixCommand
    public AjaxResult auditPass(@PathVariable @Validated @NotBlank(message = "采购单ID不能为空") String purchaseId) {
        return purchaseService.auditPass(purchaseId);
    }

    /**
     * 审核不通过【根据采购单号】
     */
    @Log(title = "采购单管理--审核不通过",businessType = BusinessType.UPDATE)
    @PostMapping("auditNoPass/{purchaseId}/{auditMsg}")
    @HystrixCommand
    public AjaxResult auditNoPass(@PathVariable @Validated @NotBlank(message = "采购单ID不能为空") String purchaseId,@PathVariable @Validated @NotBlank(message = "审核信息不能为空") String auditMsg) {
        return purchaseService.auditNoPass(purchaseId, auditMsg);
    }

    /**
     * 根据采购单据ID查询采购详情信息
     */
    @GetMapping("getPurchaseItemById/{purchaseId}")
    @HystrixCommand
    public AjaxResult getPurchaseItemById(@PathVariable @Validated @NotBlank(message = "采购单ID不能为空") String purchaseId) {
        return AjaxResult.success(purchaseService.getPurchaseItemById(purchaseId));
    }

    /**
     * 暂存采购单和详情
     */
    @Log(title = "采购单管理--暂存采购单和详情",businessType = BusinessType.INSERT)
    @PostMapping("addPurchase")
    @HystrixCommand
    public AjaxResult addPurchase(@RequestBody PurchaseFormDto purchaseFormDto) {
        SimpleUser simpleUser = ShiroUtils.getCurrentSimpleUser();
        purchaseFormDto.getPurchaseDto().setSimpleUser(simpleUser);
        return purchaseService.addPurchase(purchaseFormDto);
    }

    /**
     * 添加采购单和详情并提交审核
     */
    @Log(title = "采购单管理--添加采购单和详情并提交审核",businessType = BusinessType.INSERT)
    @PostMapping("addPurchaseToAudit")
    @HystrixCommand
    public AjaxResult addPurchaseToAudit(@RequestBody PurchaseFormDto purchaseFormDto) {
        SimpleUser simpleUser = ShiroUtils.getCurrentSimpleUser();
        purchaseFormDto.getPurchaseDto().setSimpleUser(simpleUser);
        return purchaseService.addPurchaseToAudit(purchaseFormDto);
    }

    /**
     * 根据采购单ID查询采购单信息和详情
     */
    @GetMapping("queryPurchaseAndItemByPurchaseId/{purchaseId}")
    @HystrixCommand
    public AjaxResult queryPurchaseAndItemByPurchaseId(@PathVariable @Validated @NotBlank(message = "采购单ID不能为空") String purchaseId) {
        return purchaseService.selectPurchaseAndItemByPurchaseId(purchaseId);
    }

    /**
     * 入库
     */
    @Log(title = "采购单管理--入库",businessType = BusinessType.INSERT)
    @PostMapping("doInventory/{purchaseId}")
    @HystrixCommand
    public AjaxResult doInventory(@PathVariable @Validated @NotBlank(message = "采购单号不能为空") String purchaseId) {
        SimpleUser simpleUser = ShiroUtils.getCurrentSimpleUser();
        return purchaseService.doInventory(purchaseId, simpleUser);
    }

    /**
     * 生成单据号
     */
    @GetMapping("generatePurchaseId")
    @HystrixCommand
    public AjaxResult generatePurchaseId() {
        return AjaxResult.success(IdGeneratorSnowflake.generatorIdWithProfix(Constants.ID_PROFIX_CG));
    }

}
